function Request(form){
	this["fields"] = {};
	if(form)
		this.addFormData(form);
}

Request.prototype = new Object();

Request.prototype.post = function(url){
	method = "post";
	var form = document.createElement("form");
	form.setAttribute("method", method);
	form.setAttribute("action", url);
	for(var key in this['fields']) {
	    var hiddenField = document.createElement("input");
	    hiddenField.setAttribute("type", "hidden");
	    
	    hiddenField.setAttribute("name", key);
	    
	    console.log("Key: " + key);
	    console.log("Value: " + (this['fields'][key]['value'] ? this['fields'][key]['value'] : ""));
	    
	    hiddenField.setAttribute("value", this['fields'][key]['value'] ? this['fields'][key]['value'] : "");
	    form.appendChild(hiddenField);
	}
	if(!document.body){
		throw new Error("Make sure to call this function after body has loaded");
	}
	document.body.appendChild(form);
	
	form.submit();
}

Request.prototype.serializeAssoc = function(form_id){
	jQuery.map($("#" + form_id).serializeArray(), function(n, i){
		this['fields'][n['name']] = n['value'];
	});
}
Request.prototype.addFormData = function(form){
	console.log("addFormData");
	var str;
	for(var i = 0; i < form.elements.length; i++)
	{
            console.log("Type: " + form.elements[i].type);
            console.log("Name: " + form.elements[i].name);
            console.log("Value: " + form.elements[i].value);
            console.log("Required: " + form.elements[i].required);
            this['fields'][form.elements[i].name] = {};
            this['fields'][form.elements[i].name]['value'] = form.elements[i].value
            this['fields'][form.elements[i].name]['required'] = form.elements[i].required ? true : false;
            this['fields'][form.elements[i].name]['id'] = form.elements[i].id;
            
	}
	
}
Request.prototype.addRequired = function(required){
	for(var i=0; i < required.length; i++){
		console.log("i: " + i);
		console.log("required[i]: " + required[i]);
		console.log(i);
		console.log(required[i]);
		if(!this['fields'][required[i]])
			this['fields'][required[i]] = {"value":"", "required":true};
		else
			this['fields'][required[i]]['required'] = true;
	}
}

Request.prototype.addFormField = function(element){
    this['fields'][element.name] = {};
    this['fields'][element.name]['value'] = element.value
    this['fields'][element.name]['required'] = element.required ? true : false;
    this['fields'][element.name]['id'] = element.id;
}
Request.prototype.addField = function(field){
    this['fields'][field.key] = field;
}

Request.prototype.validate = function(){
	var missingFields = new Array();
	for(var key in this["fields"]){
		var field = this["fields"][key];
		field['error'] = false;	
		if(field['required'] && (!field["value"] || field["value"] == "")){
			missingFields.push(key);
			field['error'] = "This field is required.";	
		}
		
	}
	
	if(!missingFields.length)
		return true;
	return {"error": "Missing fields: " + missingFields.join(",")};	
}

Request.prototype.highlightErrors = function(){
	for(var key in this["fields"]){
		var field = this["fields"][key];
		if(field['id']){
			if(field['error'])
				document.getElementById(field['id']).className += " field-error";
			else
				document.getElementById(field['id']).className = document.getElementById(field['id']).className.replace(/\bfield-error\b/,'')
		}
	}
	
}

